#!/usr/bin/env python
# encoding: utf-8
"""
NewGameFrame.py

Created by Steve Streza on 2008-09-24.
Copyright (c) 2008 __MyCompanyName__. All rights reserved.
"""

import sys
import string
import os
from Tkinter import *

class NewGameFrame(Frame):
	"""frame to allow the user to make a new game. it is inserted into
	   the GameInputView. callsback after input is collected
	"""
	
	def __init__(self,master,doneHandler,**kv):
		"""constructor. doneHandler is the callback method"""
		Frame.__init__(self,master,kv)
		
		self.doneHandler = doneHandler
		
		self.makerLabel   = Label(self, text="Maker: ")
		self.breakerLabel = Label(self, text="Breaker: ")
		self.logLabel	  = Label(self, text="Log: ")
		self.delayLabel   = Label(self, text="Delay: ")
		
		self.firstDividerLine  = Frame(self, height=1, bd=1, relief=SUNKEN)
		self.secondDividerLine = Frame(self, height=1, bd=1, relief=SUNKEN)

		self.makerValue = IntVar()
		self.breakerValue = IntVar()

		self.manMakerRadio   = Radiobutton(self, text="Human",    variable=self.makerValue, command=self.handleHumanMaker, value=0)
		self.cpuMakerRadio   = Radiobutton(self, text="Computer", variable=self.makerValue, command=self.handleComputerMaker, value=1)
		self.netMakerRadio   = Radiobutton(self, text="Network",  variable=self.makerValue, command=self.handleNetworkMaker, value=2)

		self.netMakerLabel = Label(self, text="IP:")
		self.netMakerEntry = Entry(self)
		self.netMakerEntry.config(state="disabled")
		
		self.manBreakerRadio = Radiobutton(self, text="Human",    variable=self.breakerValue, command=self.handleHumanBreaker, value=0)
		self.cpuBreakerRadio = Radiobutton(self, text="Computer", variable=self.breakerValue, command=self.handleComputerBreaker, value=1)
		self.netBreakerRadio = Radiobutton(self, text="Network",  variable=self.breakerValue, command=self.handleNetworkBreaker, value=2)

		self.makerValue.set(0)
		self.breakerValue.set(0)
		
		def handleBreakerSet(*args):
			"""docstring for handleBreakerSet"""
			self.handleBreakerChange()
			pass
		
		def handleMakerSet(*args):
			"""docstring for handleMakerSet"""
			self.handleMakerChange()
			pass

		self.makerValue.trace("w",handleMakerSet)
		self.breakerValue.trace("w",handleBreakerSet)
		
		self.netBreakerLabel = Label(self, text="IP:")
		self.netBreakerEntry = Entry(self)
		self.netBreakerEntry.config(state="disabled")
		
		self.logEntry = Entry(self)
		self.delayEntry = Entry(self)
		
		self.doneButton = Button(self, text="New Game", command=self.handleDone)

		self.makerLabel.grid(row=0, column=0)
		self.manMakerRadio.grid(row=0, column=1)
		self.cpuMakerRadio.grid(row=0, column=2)
		self.netMakerRadio.grid(row=0, column=3)
		
		self.netMakerLabel.grid(row=1, column=0)
		self.netMakerEntry.grid(row=1, column=1, columnspan=3)
		
		self.firstDividerLine.grid(row=2, columnspan=5, sticky="WE")
		
		self.breakerLabel.grid(row=3, column=0)
		self.manBreakerRadio.grid(row=3, column=1)
		self.cpuBreakerRadio.grid(row=3, column=2)
		self.netBreakerRadio.grid(row=3, column=3)
		
		self.netBreakerLabel.grid(row=4, column=0)
		self.netBreakerEntry.grid(row=4, column=1, columnspan=3)
		
		self.secondDividerLine.grid(row=5, columnspan=5, sticky="we")
		
		self.logLabel.grid(row=6, column=0)
		self.logEntry.grid(row=6, column=1, columnspan=3)
		
		self.delayLabel.grid(row=7,column=0)
		self.delayEntry.grid(row=7,column=1, columnspan=2)
		self.doneButton.grid(row=7,column=3)
	
	def handleDone(self):
		"""callback"""
		waitTimeStr = self.delayEntry.get()
		waitTime = 0
		if waitTimeStr != "":
			waitTime = string.atoi(waitTimeStr,10)
		
		maker = self.makerValue.get()
		makerIP = self.netMakerEntry.get()
		breaker = self.breakerValue.get()
		breakerIP = self.netBreakerEntry.get()
		if breaker == 0:
			waitTime = 0
		logPath = self.logEntry.get()
		if logPath == "":
			logPath = None
		handler = self.doneHandler
		handler(waitTime, maker, breaker, logPath, makerIP, breakerIP)
		
	def handleBreakerChange(self):
		"""docstring for handleBreakerChange"""
		newState = "normal"
		if self.breakerValue.get() == 2:
                    pass
                else:
                    newState = "disabled"
		self.netBreakerEntry.config(state=newState)
		
	def handleMakerChange(self):
		"""docstring for handleMakerChange"""
		newState = "normal"
		if self.makerValue.get() == 2:
                    pass
                else:
                    newState = "disabled"
		self.netMakerEntry.config(state=newState)

	def handleHumanBreaker(self):
		"""docstring for handleNetworkBreaker"""
		self.breakerValue.set(0)

	def handleComputerBreaker(self):
		"""docstring for handleNetworkBreaker"""
		self.breakerValue.set(1)

	def handleNetworkBreaker(self):
		"""docstring for handleNetworkBreaker"""
		self.breakerValue.set(2)

	def handleHumanMaker(self):
		"""docstring for handleNetworkBreaker"""
		self.makerValue.set(0)

	def handleComputerMaker(self):
		"""docstring for handleNetworkBreaker"""
		self.makerValue.set(1)

	def handleNetworkMaker(self):
		"""docstring for handleNetworkBreaker"""
		self.makerValue.set(2)
